package com.sui.bigdata.flink.sql.function.scalar;

import com.feidee.fdcommon.util.ToolUtils;
import org.apache.flink.table.functions.ScalarFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author YongChen
 * @date 2018/12/25 10:18
 * @description
 * @email yong_chen@sui.com
 */
public class UnixTimestamp extends ScalarFunction {
    private static final Logger LOG = LoggerFactory.getLogger(UnixTimestamp.class);
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Long eval(String message) throws Exception{
        if (!ToolUtils.isNotNull(message)){
            return 0L;
        }
        return sf.parse(message).getTime();// 日期转换为时间戳
    }

    public String eval(Long timeStamp) throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeStamp * 1000);//转换为毫秒
        Date date = calendar.getTime();
        String dateString = sf.format(date);//时间戳转换为日期
        return dateString;
    }
}
